<template>
	<view>
	  <template v-if="list.length>0">
	   <view class="cart-box">
			<view class="cart-item" v-for="(item, index) in list" :key="index">
				<view class="cart-item-checkbox" @click="toggleSelect(index)">
					<image v-if="item.isCheck" src="@/static/radio@selected.png"></image>
					<image v-else src="@/static/radio.png"></image>
				</view>
				
				<view class="cart-item-img">
					<image class="good-image" :src="item.image"></image>
				</view>
				<view class="cart-item-word">
					<view class="title-box">
						<text class="title">{{item.name}}</text>
						<text class="title-price">￥{{item.price}}</text>
					</view>
					<view class="title-box">
						<text class="title">{{item.props_text}}</text>
					</view>
					<view class="bottom-box">
						<view class="cart-item-counts">
							<view class="btns"
								@click="changeNum(index, 'cut')" 
								data-type="cut">-</view>
							<view class="counts">{{item.num}}</view>
							<view class="btns" @click="changeNum(index, 'inc')" data-type="add">+</view>
						</view>
						<view class="delete" @click="del(index)">×</view>
					</view>
				</view>
			</view>
	   </view>
	
	    <view class="footer-account-box all-accounts-box">
			<view class="all-select" @click="toggleSelectAll">
				<image v-if="checkAll" class="title-icon" src="@/static/radio@selected.png"></image>
				<image v-else class="title-icon" src="@/static/radio.png"></image>
				
				<text>全选({{totalCount}})</text>
			</view>
			<view class="all-price-submit">
				<view class="price-text"><text style="color: #979797;">合计:</text><text style="color: #CD3537;">￥{{totalPrice}}</text></view>
				<view class="arrow-icon" @click="submitOrder">
					<view>去结算</view>
					<!-- <image src="../../imgs/icon/arrow@grey.png"></image> -->
				</view>
			</view>
	    </view>
	
	  </template> 
	  <template v-else>
		  <view class="no-data">
				<image src="../../static/no-thing.png"></image>
				<text>购物车为空</text>
				<text>赶紧去逛逛，购买心仪的商品吧</text>
				<navigator url="/pages/index/index" open-type="switchTab" class="p-b-btn active">
					<view>去逛逛</view>
				</navigator>
		  </view>
	  </template>
	  
	</view>
</template>

<script>
	import { Cart } from '@/utils/model/cart-model.js';
	var cart = new Cart();
	
	export default {
		data() {
			return {
				list:[],
				checkAll: false,
				totalPrice: 0.00,
				totalCount: 0
			}
		},
		onHide() {
			this.checkAll = false
		},
		onShow(){
			let that = this;
			cart.getList().then(res => {
				console.log('aaaaaaaaaaaaaaaaaaaaaa')
				console.log(res)
				if(res.code == 1){
					that.list = res.data.list;
					for (var i = 0; i < that.list.length; i++) {
						that.list[i].isCheck = false;
					}
				}
				
				//计算总价
				that._calcTotalPriceAndTotalCount();
			}).catch(err => {
				//that.$u.toast(err.msg);
				console.log('eeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
				console.log(err)
				uni.showToast({
					title: err.msg
				})
			})
		},
		methods: {
			/*删除商品*/
			del(index){
				let that = this;
				let id = that.list[index].id;
				cart.delete(id).then(res => {
					console.log('aaaaaaaaaaaaaaaaaaaaaa')
					console.log(res)
					that.list.splice(index, 1);
					//计算总价
					that._calcTotalPriceAndTotalCount();
				}).catch(err => {
					//that.$u.toast(err.msg);
					console.log('eeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
					console.log(err)
				})
				
				
			},
			/*调整商品数目*/
			changeNum(index,type){
				let that = this;
				let id = that.list[index].id;
				let num = that.list[index].num;
				if(type=='inc') {
					num++;
				}else{
					if(that.list[index].num < 2){
						return
					}
					num--;
				}
				
				cart.update(id, num).then(res => {
					console.log('aaaaaaaaaaaaaaaaaaaaaa')
					console.log(res)
					that.list[index].num = num;
					//计算总价
					that._calcTotalPriceAndTotalCount();
				}).catch(err => {
					//that.$u.toast(err.msg);
					console.log('eeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
					console.log(err)
				})
			},
			
			//单选
			toggleSelect(index){
				let that = this;
				that.checkAll = false;
				that.totalCount = 0;
				
				that.list[index].isCheck = !that.list[index].isCheck;
				let tempNum = 0;
				for (var i = 0; i < that.list.length; i++) {
					if(that.list[i].isCheck){
						tempNum++;
					}
				}
				that.totalCount = tempNum;
				if(tempNum == that.list.length){
					that.checkAll = true;
				}
				//计算总价
				that._calcTotalPriceAndTotalCount();
				this.$forceUpdate();
			},
			/*全选*/
			toggleSelectAll(){
				let that = this;
				that.checkAll = !that.checkAll;
				if(that.checkAll){
					for (var i = 0; i < that.list.length; i++) {
						that.list[i].isCheck = true;
					}
					that.totalCount = that.list.length;
				}else{
					
					for (var i = 0; i < that.list.length; i++) {
						that.list[i].isCheck = false;
					}
					that.totalCount = 0;
				}
				that._calcTotalPriceAndTotalCount();
			},
			
			
			/* 
			*	计算总金额和选择的商品总数
			* */
			_calcTotalPriceAndTotalCount(){
				let totalPrice = 0;
				let totalCount = 0;
				let multiple = 100;
				let list = this.list;
				let leg = list.length;
				
				console.log(list);
				for(let i = 0; i < leg; i++){
					//避免 0.05 + 0.01 = 0.060 000 000 000 000 005 的问题，乘以 100 *100
					if(list[i].isCheck) {
						totalPrice += Number(list[i].price) * multiple * list[i].num * multiple;
						totalCount += list[i].num;
					}
				}
				this.totalCount = totalCount;
				this.totalPrice = totalPrice / (multiple*multiple);
			},	
			submitOrder() {
				/* let that = this;
				let list = [];
				for (var i = 0; i < that.list.length; i++) {
					if(that.list[i].isCheck){
						list.push(that.list[i]);
					}
				}
				if(list.length == 0){
					uni.showToast({
						title:'请选择产品'
					})
					return
				} 
				uni.navigateTo({
					url: '/pages/order/add?list='+ JSON.stringify(list)+'&account='+this.totalPrice
				});
				*/
				var ids = "";
				for (var i = 0; i < this.list.length; i++) {
					if (this.list[i].isCheck === true) {
						ids += this.list[i].id + ",";
					}
				}
				ids = ids.substr(0, ids.length - 1);
				if (ids.length === 0) {
					uni.showToast({
						title:'请选择至少一件商品'
					})
					return;
				}
				/* uni.navigateTo({
					url: '/pages/order/add?ids='+ ids +'&account='+this.totalPrice
				}); */
				uni.navigateTo({
					url:'/pages/order/add?ids='+ ids + '&is_new=0'
				})
			},
		
		}
	}
</script>

 
<style>
	page{
		background-color: #F5F5F5;
	}
</style>
 
<style scoped lang="scss">
	@import '~@/pages/cart/index.scss';
</style>
